package dotsandboxs;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * MenuMonitor.java 实现了ActionListener接口，作为界面菜单事件监听器
 */
public class MenuMonitor implements ActionListener {
    ChessGUI gui;
    ObjectOutputStream OOS = null;
    ObjectInputStream OIS = null;
    File file = null;

    /**
     * 监听器类带参数的构造方法
     *
     * @param gui 界面类对象
     */
    public MenuMonitor(ChessGUI gui) {
        this.gui = gui;
    }

    /**
     * 实现actionPerformed方法
     *
     * @param e 动作事件对象
     */
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == gui.menuItemOpen) {
            // 打开
            JFileChooser chooser = new JFileChooser(".\\");// 当前目录
            FileNameExtensionFilter filter = new FileNameExtensionFilter("文本", "txt");// 文本筛选器
            chooser.setFileFilter(filter);
            chooser.showOpenDialog(null);
            try {
                OIS = new ObjectInputStream(new FileInputStream(chooser.getSelectedFile().getAbsolutePath()));
                // 读取文件（这个过程将重新实例化一个窗口）
                Status status = (Status) OIS.readObject();
                copy(status);
                // 调用重绘函数
                gui.frame.repaint();
            } catch (IOException | ClassNotFoundException e1) {
                e1.printStackTrace();
            } finally {
                if (OIS != null) {
                    try {
                        OIS.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }

        } else if (e.getSource() == gui.menuItemSave) {
            // 保存
            String name = JOptionPane.showInputDialog(gui.frame, "请输入文件名：");
            file = new File(name + ".txt");
            try {
                OOS = new ObjectOutputStream(new FileOutputStream(file));
                OOS.writeObject(ChessGUI.list.get(ChessGUI.list.size() - 1));
                // 检测文件是否保存成功并弹窗
                if (file.exists()) {
                    JOptionPane.showMessageDialog(gui.frame, "保存成功！", "保存", JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(gui.frame, "保存失败！请重新保存", "保存", JOptionPane.ERROR_MESSAGE);
                }
            } catch (IOException e1) {
                e1.printStackTrace();
            } finally {
                if (OOS != null) {
                    try {
                        OOS.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }

        } else if (e.getSource() == gui.menuItemClear ) {
            // 清空
            clear();
        } else if (e.getSource() == gui.menuItemRevoke) {

            // 撤销
            if (ChessGUI.list.size() == 0) {
                JOptionPane.showMessageDialog(gui.frame, "没有前一步了", "悔棋失败", JOptionPane.ERROR_MESSAGE);
            } else {
                ChessGUI.list.remove(ChessGUI.list.size() - 1);
                if (ChessGUI.list.size() == 0) {
                    clear();
                } else {
                    Status status = ChessGUI.list.get(ChessGUI.list.size() - 1);
                    copy(status);
                    // 调用重绘函数
                    gui.frame.repaint();
                }
            }

        }else if ( e.getSource() == gui.menuItemStart) {
            // 开始
            clear();
            new StartMenu(gui);
        }
    }

    /**
     * 复制函数，将从列表里读取出来的状态类对象的成员方法，复制一份给界面类对象
     *
     * @param status 状态类对象
     */
    void copy(Status status) {
        System.arraycopy(status.block, 0, ChessGUI.block, 0, status.block.length);
        System.arraycopy(status.redBox, 0, ChessGUI.redBox, 0, status.redBox.length);
        System.arraycopy(status.blueBox, 0, ChessGUI.blueBox, 0, status.blueBox.length);
        System.arraycopy(status.side, 0, ChessGUI.side, 0, status.side.length);
        System.arraycopy(status.redSide, 0, ChessGUI.redSide, 0, status.redSide.length);
        System.arraycopy(status.blueSide, 0, ChessGUI.blueSide, 0, status.blueSide.length);
        ChessGUI.list.removeAll(ChessGUI.list);
        for (int i = status.list.size() - 1; i > 0; i--) {
            ChessGUI.list.add(status.list.get(i));
        }
        ChessGUI.color = status.color;
        ChessGUI.redBlock = status.redBlock;
        ChessGUI.blueBlock = status.blueBlock;
        ChessGUI.sideCount = status.sideCount;
        ChessGUI.count = status.count;
        ChessGUI.redSideCount = status.redSideCount;
        ChessGUI.blueSideCount = status.blueSideCount;
        ChessGUI.boxCount = status.boxCount;
        ChessGUI.redBoxCount = status.redBoxCount;
        ChessGUI.blueBoxCount = status.blueBoxCount;
        ChessGUI.gameMode = status.gameMode;
    }

    /**
     * 清空函数 将界面类的成员变量赋初值
     */
    void clear() {
        ChessGUI.list.removeAll(ChessGUI.list);
        Arrays.fill(ChessGUI.block, 0);
        Arrays.fill(ChessGUI.redBox, 0);
        Arrays.fill(ChessGUI.blueBox, 0);
        Arrays.fill(ChessGUI.side, 0);
        Arrays.fill(ChessGUI.redSide, 0);
        Arrays.fill(ChessGUI.blueSide, 0);
        ChessGUI.color = 0;
        ChessGUI.redBlock = 0;
        ChessGUI.blueBlock = 0;
        ChessGUI.sideCount = 0;
        ChessGUI.count = 0;
        ChessGUI.redSideCount = 0;
        ChessGUI.blueSideCount = 0;
        ChessGUI.boxCount = 0;
        ChessGUI.redBoxCount = 0;
        ChessGUI.blueBoxCount = 0;
        ChessGUI.gameMode = 0;
        // 调用重绘函数
        gui.frame.repaint();
    }
}
